package com.sunreal.paygateway.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sunreal.paygateway.sys.dao.model.SysUser;
import com.sunreal.paygateway.sys.service.SysUserService;
import com.sunreal.paygateway.sys.util.GsonUtil;
import com.sunreal.paygateway.sys.util.result.WebResult;

/**
 * 功能描述：//TODO
 *
 * @Author 彭广
 * @Date 2019/1/22
 */
@Controller
public class LoginController {
    private Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private SysUserService sysUserService;


    @RequestMapping(value = {"/"})
    public String view() {
        return "index.html";
    }

    @RequestMapping("/main")
    public String showHome(HttpSession session) {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        logger.info("当前登陆用户：" + GsonUtil.toJson(authentication));
//        String name = authentication.getName();
//        if (!name.equalsIgnoreCase("anonymousUser")) {
//            logger.info("当前登陆用户：" + name);
//            session.setAttribute("USER", authentication.getPrincipal());
//        }
//        logger.info("从session中获取的：" + GsonUtil.toJson(session.getAttribute("USER")));
        logger.info("这是登录成功了吧！");
        return "main.html";
    }

    @RequestMapping("/test")
    public String test(HttpSession session) {
        logger.info("这就是个测试");
        return "这就是个测试";
    }

    /**
     * 用户登录
     *
     * @param jsonObj userName 用户名
     * @param jsonObj password 密码
     * @return 结果
     */
    @RequestMapping("/login")
    @ResponseBody
    public Object login(@RequestBody JSONObject jsonObj) {
        WebResult result = new WebResult();
        try {
            String userName = jsonObj.getObject("userName", String.class);
            String password = jsonObj.getObject("password", String.class);
            if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
                result.isWrong();
                result.setMsg("用户名密码不允许为空！");
                return result;
            }
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            if (subject.isAuthenticated()) {
                SysUser info = (SysUser) SecurityUtils.getSubject().getPrincipal();
                //返回结果
                result.isOK();
                result.setData(info);
                result.setMsg("登录成功！");
//                return result;
                return "/main";
            } else {
                result.isWrong();
                result.setMsg("登录失败！");
                return result;
            }
        } catch (UnknownAccountException e) {
            result.isWrong();
            result.setMsg("账号或者密码错误！");
            return result;
        } catch (Exception e) {
            logger.error("exception", e);
            result.setException(e);
            result.isWrong();
            result.setMsg("登录失败！");
            return result;
        }
    }

    @RequestMapping("/admin")
    @ResponseBody
    public String printAdmin() {
        return "如果你看见这句话，说明你有ROLE_ADMIN角色";
    }

    @RequestMapping("/user")
    @ResponseBody
    public String printUser() {
        return "如果你看见这句话，说明你有ROLE_USER角色";
    }
}
